﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.BLL;
using RFIDStockManagement.Common.Public.Enums;

namespace RFIDStockManagement.Client
{
    public partial class MeasureEdit : BaseForm
    {
        private MeasureInfo m_MeasureInfo = null;
        private long m_Id = 0;
        private long m_MeasureGroupId = 0;
        public MeasureEdit(long measureGroupId, long id)
        {
            InitializeComponent();
            m_MeasureGroupId = measureGroupId;
            m_Id = id;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Trim().Length == 0 || 
                    txtNumber.Text.Trim().Length == 0 ||
                    txtRate.Text.Trim().Length == 0)
                {
                    AlertForm.ShowMessage("单位名称、编码、换算率都不能为空");
                    return;
                }
                decimal rate = 1;
                if (decimal.TryParse(txtRate.Text.Trim(), out rate) == false)
                {
                    AlertForm.ShowMessage("换算率不合法");
                    return;
                }

                if (m_MeasureInfo == null)
                {
                    Dictionary<string, object> filter = new Dictionary<string, object>();
                    filter.Add("FMeasureGroupID", m_MeasureGroupId);
                    filter.Add("FIsBase", true);
                    DataTable dt = BasicInfoManager.GetList(filter, EnumBasicInfo.Measure);
                    if (dt != null && dt.Rows.Count > 0 && chkIsBase.Checked == true)
                    {
                        AlertForm.ShowMessage(string.Format("已经存在基本单位“{0}”，请修改换算率重新保存", dt.Rows[0]["FName"]));
                        chkIsBase.Checked = false;
                        txtRate.Enabled = true;
                        return;
                    }
                    m_MeasureInfo = new MeasureInfo();
                    m_MeasureInfo.MeasureGroupId = m_MeasureGroupId;
                }
                m_MeasureInfo.Name = txtName.Text.Trim();
                m_MeasureInfo.Number = txtNumber.Text.Trim();
                m_MeasureInfo.IsBase = chkIsBase.Checked;
                m_MeasureInfo.Rate = rate;

                m_MeasureInfo = BasicInfoManager.Save(m_MeasureInfo, EnumBasicInfo.Measure) as MeasureInfo;

                AlertForm.ShowMessage("保存成功");
                this.Close();
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }

        private void MeasureEdit_Load(object sender, EventArgs e)
        {
            if (m_Id > 0)
            {
                BindData(m_Id);
            }
            else
            {
                Dictionary<string, object> filter = new Dictionary<string, object>();
                filter.Add("FMeasureGroupID", m_MeasureGroupId);
                DataTable dt = BasicInfoManager.GetList(filter, EnumBasicInfo.Measure);
                if (dt == null || dt.Rows.Count == 0)
                {
                    chkIsBase.Checked = true;
                    chkIsBase.Enabled = false;
                    txtRate.Text = "1";
                    txtRate.Enabled = false;
                }
                else
                {
                    chkIsBase.Checked = false;
                    chkIsBase.Enabled = false;
                }
            }
        }

        private void BindData(long id)
        {
            try
            {
                m_MeasureInfo = BasicInfoManager.GetOne(id, EnumBasicInfo.Measure) as MeasureInfo;
                if (m_MeasureInfo == null)
                {
                    AlertForm.ShowMessage("读取不出数据，可能被删除");
                    this.Close();
                    return;
                }
                txtName.Text = m_MeasureInfo.Name;
                txtNumber.Text = m_MeasureInfo.Number;
                txtRate.Text = m_MeasureInfo.Rate.ToString();
                chkIsBase.Checked = m_MeasureInfo.IsBase;
                chkIsBase.Enabled = false;
                if (m_MeasureInfo.IsBase || m_MeasureInfo.BeQuote)
                {
                    txtRate.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
                this.Close();
            }
        }
    }
}